package cn.tomoki.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import cn.tomoki.config.URL;
import cn.tomoki.config.UserId;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

//个人信息
public class PersonalInformationActivity extends AppCompatActivity {
    private TextView persongalName;
    private TextView persongalProperty;
    private TextView persongalTextId;
    private TextView persongalTextGrade;
    private TextView persongalTextPhone;
    private TextView persongalTextAddress;
    private Button leverButton;
    private String nameId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_information);
        persongalName = this.findViewById(R.id.personalName);
        persongalProperty = this.findViewById(R.id.personalProperty);
        persongalTextId = this.findViewById(R.id.personalTextId);
        persongalTextGrade = this.findViewById(R.id.personalTextGrade);
        persongalTextPhone = this.findViewById(R.id.personalTextPhone);
        persongalTextAddress = this.findViewById(R.id.personalTextAddress);
        leverButton = findViewById(R.id.leaveButton);
        serListeners();
        nameId = UserId.UserId;
        System.out.println("nameid是："+nameId);

        interfaceModule();

        ImageButton myBreak =findViewById(R.id.breakButton);
        myBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void serListeners() {
        leverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String url = URL.url+"/user/logout";
                            FormBody.Builder params = new FormBody.Builder();
                            OkHttpClient client = new OkHttpClient()
                                    .newBuilder()
                                    .connectTimeout(200, TimeUnit.SECONDS)
                                    .readTimeout(250, TimeUnit.SECONDS)
                                    .build();
                            Request request = new Request.Builder()
                                    .url(url)
                                    .post(params.build())
                                    .build();
                            Response response = client.newCall(request).execute();
                            String responseDate = response.body().string();
                            JSONObject jsonObject = new JSONObject(responseDate);
                            String state = jsonObject.getString("state");
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }).start();
                Intent intent = new Intent(PersonalInformationActivity.this,MainActivity.class);
                OutUserActivity.instance.finish();
                finish();
                startActivity(intent);
            }
        });
    }

    private void interfaceModule() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
//                    String id = String.valueOf(number);
                    FormBody.Builder params = new FormBody.Builder();
                    params.add("outUserId",nameId);
                    String url = URL.url+"/outuser/get";
                    //创造HTTP客户端
                    OkHttpClient client = new OkHttpClient()
                            .newBuilder()
                            .connectTimeout(200, TimeUnit.SECONDS)
                            .readTimeout(250, TimeUnit.SECONDS)
                            .build();
                    //创造http请求
                    Request request = new Request.Builder()
                            .url(url)
                            .post(params.build())
                            .build();
                    //执行发送的指令
                    Response response = client.newCall(request).execute();
                    //获取后端返回的结果
                    String responseDate = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseDate);
                    String name = jsonObject.getString("name");
                    String sex = jsonObject.getString("sex");
                    String tellPhone = jsonObject.getString("tellphone");
                    String address = jsonObject.getString("address");
                    String department = jsonObject.getString("department");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            persongalName.setText(name);
                            persongalProperty.setText(department);
                            persongalTextPhone.setText(tellPhone);
                            persongalTextAddress.setText(address);
                            persongalTextGrade.setText(sex);
                            persongalTextId.setText(nameId);
                            Toast.makeText(PersonalInformationActivity.this,"姓名"+name,Toast.LENGTH_LONG).show();
                        }
                    });

                }catch (Exception e){
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(PersonalInformationActivity.this,"连接不上网络啦",Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    }
}